package controller

import (
	"encoding/json"
	"errors"
	"fmt"
	"gin_scaffold/common"
	"gin_scaffold/dao"
	"gin_scaffold/dto"
	"gin_scaffold/lib"
	"gin_scaffold/middleware"
	"github.com/gin-gonic/gin"
	"regexp"
	"strconv"
)

type HomeController struct {
}

func HomeNologinRegister(router *gin.RouterGroup) {
	home := &HomeController{}
	router.POST("/wxlogin", home.wxlogin)
	router.GET("/getVersion", home.getVersion)
	router.GET("/getUnion", home.getUnion)
}
func HomeRegister(router *gin.RouterGroup) {
	home := &HomeController{}
	router.POST("/verify", home.verify)
}

func (home *HomeController) getVersion(c *gin.Context) {
	data := c.Query("platform")
	version := c.Query("version")
	platform := []string{"ios", "android"}
	var ptest bool = false
	for i, _ := range platform {
		if platform[i] == data {
			ptest = true
			break
		}
	}
	if !ptest {
		middleware.ResponseError(c, 400, errors.New("参数错误!"))
		return
	}
	if version == "" {
		middleware.ResponseError(c, 400, errors.New("平台版本未传!"))
		return
	}
	reg, _ := regexp.Compile("\\.")
	versionAlias := reg.ReplaceAllString(version, "")
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	v, err := (&dao.Version{}).Find(c, tx, data)
	if err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	fmt.Println(v, versionAlias)
	need := "版本太旧"
	int, _ := strconv.Atoi(versionAlias)
	if int >= v.VersionAlias {
		need = "版本最新"
	}
	middleware.ResponseSuccess(c, need)
}

func (home *HomeController) getUnion(c *gin.Context) {
	code := c.Query("code")
	if code == "" {
		middleware.ResponseError(c, 2001, errors.New("没有传code"))
		return
	}
	secret := lib.GetStringConf("base.base.secret")
	appid := lib.GetStringConf("base.base.appid")
	url := "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"
	res := common.HttpGet(url)
	type reqRes struct {
		Errcode int    `json:"errcode"`
		Errmsg  string `json:"errmsg"`
		Rid     string `json:"rid"`
	}
	var data reqRes
	res2 := json.Unmarshal([]byte(res), &data)
	fmt.Println(res2, res, data)
	middleware.ResponseSuccess(c, data)
}

// ListPage godoc
// @Summary 微信登录
// @Description 微信登录
// @Tags 用户
// @ID /wxlogin
// @Accept  json
// @Produce  json
// @Param polygon body dto.Wxlogin true "body"
// @Success 200 {object} middleware.Response{data=common.DetailRes} "success"
// @Router /wxlogin [post]
func (home *HomeController) wxlogin(c *gin.Context) {
	var need = dto.Wxlogin{}
	if err := need.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 400, err)
		return
	}
	realIp := c.ClientIP()
	detail := common.Logindetail(c, need, "wxsmall", realIp, need.Platform, "")
	middleware.ResponseSuccess(c, detail)
}

func (home *HomeController) verify(c *gin.Context) {
	//token头的参数
	//token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiIyM2ZhYWQ4My02YTMwLTRkMDUtODk2MS1hZWJiY2JkY2VhMWMifQ.waDbdxWel5HHg47iUAmOUj9IrTwqiEvlRORJTbs9en8"
	//获取uid用来验证
	uid, _ := c.Get("uid")
	fmt.Println(uid)
	middleware.ResponseSuccess(c, uid)
}
